using UnityEngine;

namespace Voodoo.Sauce.Internal.Ads
{
	public class InterstitialAdDisplayConditions
	{
		private const string TAG = "AdDisplayConditions";

		private const string PrefsFirstAppLaunch = "VoodooSauce.Interstitial.FirstAppLaunch";

		private const int DefaultDelayInSecondsBeforeFirstInterstitialAdAd = 30;

		private const int DefaultDelayInSecondsBetweenInterstitialAds = 30;

		private const int DefaultMaxGamesBetweenInterstitialAds = 3;

		private const int DefaultDelayInSecondsBetweenRewardedVideoAndInterstitial = 5;

		private readonly int _delayInSecondsBeforeFirstInterstitialAd;

		private readonly int _delayInSecondsBetweenInterstitialAds;

		private readonly int _maxGamesPlayedBetweenInterstitials;

		private bool _firstInterstitialDisplayed;

		private int _gamesPlayedSinceLastInterstitial;

		private float _lastInterstitialTime;

		private bool _rewardedVideosDelayFullscreen;

		private float _lastRewardedVideoTime;

		public InterstitialAdDisplayConditions()
			: this(30, 30, 3)
		{
		}

		public InterstitialAdDisplayConditions(int delayInSecondsBeforeFirstInterstitialAdAd, int delayInSecondsBetweenInterstitialAds, int maxGamesBetweenInterstitialAds)
		{
			_delayInSecondsBeforeFirstInterstitialAd = delayInSecondsBeforeFirstInterstitialAdAd;
			_delayInSecondsBetweenInterstitialAds = delayInSecondsBetweenInterstitialAds;
			_maxGamesPlayedBetweenInterstitials = maxGamesBetweenInterstitialAds;
			VoodooLog.Log("AdDisplayConditions", "Interstitial ads display conditions set to : " + this);
			_firstInterstitialDisplayed = PlayerPrefs.HasKey("VoodooSauce.Interstitial.FirstAppLaunch");
			_gamesPlayedSinceLastInterstitial = 0;
			_lastInterstitialTime = -1f;
			_lastRewardedVideoTime = -1f;
			_rewardedVideosDelayFullscreen = true;
			PlayerPrefs.SetInt("VoodooSauce.Interstitial.FirstAppLaunch", 0);
		}

		public bool AreConditionsMet()
		{
			if (!_firstInterstitialDisplayed)
			{
				if (Time.unscaledTime < (float)_delayInSecondsBeforeFirstInterstitialAd)
				{
					VoodooLog.Log("AdDisplayConditions", "Not showing interstitial (only played " + Time.unscaledTime + "s / " + _delayInSecondsBeforeFirstInterstitialAd + "s before first interstitial).");
					return false;
				}
				VoodooLog.Log("AdDisplayConditions", "Showing first interstitial after " + Time.unscaledTime + "s / " + _delayInSecondsBeforeFirstInterstitialAd + "s");
				return true;
			}
			VoodooLog.Log("AdDisplayConditions", "Time since last interstitial ad : " + (Time.unscaledTime - _lastInterstitialTime) + "s / " + _delayInSecondsBetweenInterstitialAds + "s");
			VoodooLog.Log("AdDisplayConditions", "Time since last rewarded video ad : " + (Time.unscaledTime - _lastRewardedVideoTime) + "s / " + 5 + "s");
			VoodooLog.Log("AdDisplayConditions", "Games played since last interstitial ad : " + _gamesPlayedSinceLastInterstitial + " / " + _maxGamesPlayedBetweenInterstitials);
			if (Time.unscaledTime - _lastInterstitialTime >= (float)_delayInSecondsBetweenInterstitialAds)
			{
				VoodooLog.Log("AdDisplayConditions", "Time since last interstitial is OK to display a new one.");
				if (!_rewardedVideosDelayFullscreen)
				{
					return true;
				}
				if (Time.unscaledTime - _lastRewardedVideoTime >= 5f)
				{
					VoodooLog.Log("AdDisplayConditions", "Time since last rewarded video is OK to display an interstitial.");
					return true;
				}
				VoodooLog.Log("AdDisplayConditions", "A rewarded video was played recently...");
			}
			if (_gamesPlayedSinceLastInterstitial >= _maxGamesPlayedBetweenInterstitials)
			{
				VoodooLog.Log("AdDisplayConditions", "Games played since last interstitial is OK to display a new one.");
				return true;
			}
			VoodooLog.Log("AdDisplayConditions", "Conditions are not met, not showing an interstitial.");
			return false;
		}

		public void InterstitialDisplayed()
		{
			_firstInterstitialDisplayed = true;
			_gamesPlayedSinceLastInterstitial = 0;
			_lastInterstitialTime = Time.unscaledTime;
		}

		public void RewardedVideoDisplayed(bool completed)
		{
			if (_rewardedVideosDelayFullscreen)
			{
				VoodooLog.Log("AdDisplayConditions", $"Rewarded Videos Delay Fullscreen is enabled. Delaying the next interstitial by {5}...");
				_lastRewardedVideoTime = Time.unscaledTime;
			}
		}

		public void GamePlayed()
		{
			_gamesPlayedSinceLastInterstitial++;
		}

		public override string ToString()
		{
			return "Interstitial display conditions = " + _delayInSecondsBeforeFirstInterstitialAd + "s before the first interstitial, " + _delayInSecondsBetweenInterstitialAds + "s OR " + _maxGamesPlayedBetweenInterstitials + " max games between two interstitial ads.";
		}
	}
}
